package com.heima.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.IArticleClient;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.SyncArticleService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class SyncArticleServiceImpl implements SyncArticleService {
    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IArticleClient articleClient;

    /**
     * 同步数据到es
     * @param vo
     */
    @Override
    public void SyncArticle(SearchArticleVo vo) {
        IndexRequest indexRequest = new IndexRequest("app_info_article");
        indexRequest.id(vo.getId().toString()).source(JSON.toJSONString(vo), XContentType.JSON);
        try {
            client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
